#ifndef LEVELPAINTER_H
#define LEVELPAINTER_H

#include "qsfmlcanvas.h"
#include "texturemanager.h"
#include "animationmanager.h"
#include "objectmanager.h"
#include "yamlstream.h"
#include <Shinygami/spriteanimate.hpp>

class LPObject : public Shiny::SpriteAnimate
{
public:
    LPObject()
    {
        _positionLock = false;
        _hasScript    = false;
    }

    void           SetObjectName(const QString& on)  { _objectName = on;     }
    const QString& ObjectName(void) const            { return _objectName;   }
    void           SetEntityName(const QString& n)   { _entityName = n;      }
    const QString& EntityName(void) const            { return _entityName;   }
    bool           PositionLocked(void) const        { return _positionLock; }
    void           SetPositionLocked(bool s)         { _positionLock = s;    }

    void           SetHasScript(bool b)              { _hasScript = b;       }
    void           SetScriptSource(const QString& s) { _scriptSource = s;    }
    void           SetScriptType(const QString& s)   { _scriptType = s;      }
    bool           HasScript(void) const             { return _hasScript;    }
    const QString& ScriptSource(void) const          { return _scriptSource; }

    void           Save(YamlStream&) const;

    sf::Vector2f   GetHalfSize(void) const
    {
	sf::IntRect  rect = GetTextureRect();
	sf::Vector2f halfSize(rect.Width / 2, rect.Height / 2);

	return (halfSize);
    }

private:
    QString _entityName;
    QString _objectName;
    QString _scriptSource;
    QString _scriptType;
    bool    _hasScript;
    bool    _positionLock;
};

class LevelPainter : public QSFMLCanvas
{
    Q_OBJECT

    class Landscape : public sf::Sprite
    {
    public:
        Landscape(QSFMLCanvas& canvas) : _world(canvas) {}
        void Update(void);

    private:
        QSFMLCanvas& _world;
    };
public:
    explicit LevelPainter(QWidget *parent = 0);

    void SetTextureManager(TextureManager* tm)      { _textureManager   = tm; }
    void SetAnimationManager(AnimationManager* am)  { _animationManager = am; }
    void SetObjectManager(ObjectManager* om)        { _objectManager    = om; }
    void ChangeLandscape(const sf::Texture*);
    void OnUpdate();

    void            Load(const QString& filepath);
    void            Save(const QString& filepath) const;
    const QString   GenerateName(void) const;

signals:
    
public slots:
    bool            AddEntity(QString);
    void            RemoveEntity(QString);

protected:
    bool            IsNameTaken(const QString&) const;

    virtual void    mousePressEvent(QMouseEvent *);
    virtual void    mouseMoveEvent(QMouseEvent *);
    virtual void    mouseReleaseEvent(QMouseEvent *);
    virtual void    mouseDoubleClickEvent(QMouseEvent *);

private:
    Landscape         _landscape;
    TextureManager*   _textureManager;
    AnimationManager* _animationManager;
    ObjectManager*    _objectManager;
    QList<LPObject>   _entities;
    LPObject*         _movingEntity;
    sf::Vector2f      _lastMovingPos;
};

#endif // LEVELPAINTER_H
